﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TagCore;
using HtmlAgilityPack;

namespace FreeDBQuery
{
    public class FreedbResponse : IMusicData
    {
        private string artist;
        private string album;
        private string title;
        private string genre;
        private uint year;
        private List<string> tracks = new List<string>();

        public FreedbResponse(IMusicData md, HtmlNode album, HtmlNode detail)
        {
            ParseResponse(album, detail);
            SetTitle(md.Title);
        }

        private void SetTitle(string title)
        {
            this.title = TagCoreUtil.MatchTitle(tracks.ToArray(), title);
        }

        private void ParseResponse(HtmlNode album, HtmlNode detail)
        {
            string artistAlbum = album.ChildNodes[0].ChildNodes[0].InnerText;

            string[] vals = artistAlbum.Split('/');
            if (vals.Length > 0)
                this.artist = vals[0].Trim();
            if (vals.Length > 1)
                this.album = vals[1].Trim();

            // set fields
            for (int loop = 0; loop < detail.ChildNodes.Count; loop++)
            {
                HtmlNode d = detail.ChildNodes[loop];
                if (loop == 0)
                    GetInfo(d);
                else
                    GetTrack(d);
            }
        }

        private void GetInfo(HtmlNode info)
        {
            string inf = info.SelectSingleNode("td/table/tr/td/div").OuterHtml;
            // lookup
            //<div style="width:100%;"><b>Tracks:</b> 3<br><b>Total time:</b> 11:00<br><b>Year:</b> 1992<br><b>Disc-ID:</b> folk / <a href="http://www.freedb.org/freedb/folk/20029403">20029403</a><br></div>
            string yr = TagCoreUtil.StringSearch(inf, "<b>Year:</b>", "<");
            if (yr.Length > 0)
                year = Convert.ToUInt32(yr);
            string gn = TagCoreUtil.StringSearch(inf, "<b>Disc-ID:</b>", "<");
            if (gn.Length > 0)
                genre = Genres.Match(new string[] { gn });
        }

        private void GetTrack(HtmlNode track)
        {
            tracks.Add(track.ChildNodes[1].InnerHtml);
        }

        private uint TrackNumber()
        {
            for (int loop = 0; loop < tracks.Count; loop++)
            {
                if (string.Compare(title, tracks[loop]) == 0)
                {
                    return (uint)(loop + 1);
                }
            }
            return 0;
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Album
        {
            get
            {
                return album;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public uint Year
        {
            get
            {
                return year;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string[] Artists
        {
            get
            {
                return new string[] { artist };
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string[] AlbumArtists
        {
            get
            {
                return new string[] { artist };
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Genre
        {
            get
            {
                return genre;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Comments
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public uint Track
        {
            get
            {
                return TrackNumber();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public byte[] Image
        {
            get
            {
                return null;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }   
}
